package com.ebelter.bpm.models.http;


import com.ebelter.bpm.models.update.UpdateService;
import com.ebelter.btcomlib.models.beans.UpdateVersionBean;
import com.ebelter.btcomlib.models.https.BaseJsonGenerator;
import com.ebelter.btcomlib.models.https.HttpsUrl;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.GsonUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;

import okhttp3.Call;
import okhttp3.Response;

public class BpmNetUtils {


    private static final String TAG = "BpmNetUtils";
    private static BpmNetUtils instance;

    private BpmNetUtils(){

    }
    public static BpmNetUtils getInstance() {
        if (instance==null){
            synchronized (NetUtils.class){
                if (instance==null) instance = new BpmNetUtils();
            }
        }
        return instance;
    }

    /**
     * post请求获取数据
     */
    public void post(Object tag, String url, StringCallback callback){
        post(tag,url,null,callback);
    }
    /**
     * post请求获取数据
     */
    public  void post(Object tag, String url, HttpParams httpParams, StringCallback callback){
        OkGo.post(url).tag(tag).params(httpParams).execute(callback);
    }

    public void getVersionJosn(Object object,final HttpResponse<UpdateVersionBean> httpResponse){
        post(object, UpdateService.getVersionJson, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.i(TAG,"服务器上的json数据："+s);
                UpdateVersionBean res= GsonUtils.getInstance().fromJson(s,UpdateVersionBean.class);
                if (res!=null){
                    httpResponse.result(true,"成功",res,s);
                }else {
                    httpResponse.result(false,"失败",null,s);
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                httpResponse.result(false,"失败",null,"onError");
            }
        });
    }
}
